var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        page: 1,
        listRows: 20,
        total: 0,
        gmtStart:'',
        gmtEnd:'',
        order:'desc',
        className:'',
        adminName:"",
        type:'4',
        idArrUnique:[],
        nochecked:[]
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page: _this.params.page,
            listRows: _this.params.listRows,
            className:_this.params.className,
            adminName:_this.params.adminName,
            order:_this.params.order,
            type:_this.params.type,
            gmtEnd:_this.params.gmtEnd,
            gmtStart:_this.params.gmtStart,
        };
        console.log(paramsObj);
        Api.newclasslist(paramsObj, function(data){
            console.log(data);
            _this.getDataList(data.list);
            _this.params.total = data.totalPages;
            layui.use('laypage', function() {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'page',
                    index: 1,
                    theme: '#3498db',
                    curr: _this.params.page,
                    count: _this.params.total, //数据总数，从服务端得到
                    hash: 'curr_page',
                    layout: ['prev', 'page', 'next', 'skip'],
                    jump: function(obj, first) {
                        _this.params.page = obj.curr;
                        //obj包含了当前分页的所有参数，比如：curr当前页
                        var Params = {
                            passport: _this.params.passport,
                            page: _this.params.page,
                            listRows: _this.params.listRows,
                            className:_this.params.className,
                            adminName:_this.params.adminName,
                            order:_this.params.order,
                            type:_this.params.type,
                            gmtEnd:_this.params.gmtEnd,
                            gmtStart:_this.params.gmtStart,
                        };
                        Api.newclasslist(Params, function(data) {
                            _this.getDataList(data.list);
                            $('input[type=checkbox][name=new]').each(function() {
                                var self=$(this);
                                _this.params.idArrUnique.forEach(function(item,index){
                                    if (self.attr('programClassId')==item) {
                                        self.iCheck('check');
                                    }
                                });
                            });
                            if ($('input[type=checkbox][name=new]:checked').length==$('#List tr').length) {
                                 $('input[type=checkbox][name=all]').parent().addClass('checked');
                            }else{
                                $('input[type=checkbox][name=all]').parent().removeClass('checked');
                            };

                        })
                    }
                });
            });
        });
    },
    getDataList: function(data) {
        var _this = this;
        if (data && data.length > 0) {
            var html = '',
                i = 0,
                len = data.length;
            for (i; i < len; i++) {
                html += '<tr>' +
                        '<td class="text-c" width="24">'+
                            '<div class="goods-checkbox">'+
                                '<div class="check-box" programClassId="' + data[i].programClassId + '">'+
                                    '<input type="checkbox" name="new" programClassId="' + data[i].programClassId + '">'+
                                '</div>'+
                            '</div>'+
                        '</td>'+
                        '<td class="text-c">' + data[i].programClassId + '</td>' +
                        '<td class="text-c">' + data[i].className + '</td>'+
                        '<td class="text-c">' + data[i].gmtCreate + '</td>'+
                        '<td class="text-c">' + data[i].operatorName + '</td>' ;

                html += '<td class="text-c">' +
                    '<p><a programClassId="' + data[i].programClassId + '"  class="handle delete">删除</a></p>' +
                    '</td>' +
                    '</tr>';


            }
            $('#List').html(html);
            $('tfoot').css('display', 'table-footer-group');
            _this.getDataEvent();
        } else {
            var html = '<tr>' +
                '<td class="text-c c-666" colspan="6" >暂无数据</td>' +
                '</tr>';
            $('#List').html(html);
            $('tfoot').css('display', 'none');
        }
    },
    getDataEvent: function() {
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('input[type=checkbox][name=all]').on('ifChanged', function() {
            if ($(this).parent().hasClass('checked')) {
                $('input[type=checkbox][name=new]').iCheck('uncheck');
                setTimeout(function(){
                    $('input[type=checkbox][name=all]').parent().addClass('unchecked');
                },100)
            } else {
                $('input[type=checkbox][name=new]').iCheck('check');
                setTimeout(function(){
                    $('input[type=checkbox][name=all]').parent().addClass('checked');
                },100)
            }
        });
        setTimeout(function(){
             $('input[type=checkbox][name=new]').on('ifChanged', function() {
                //获取选取的并加入数组
                _this.params.idArrUnique.push($(this).attr('programClassId'));

                //数组去重
                _this.params.idArrUnique=_this.params.idArrUnique.unique();

                // console.log(_this.params.idArrUnique);

                //获取未选取的并加入数组
                for (var i = 0; i < $('input[type=checkbox][name=new]').length; i++) {
                     if ($('input[type=checkbox][name=new]').eq(i).prop('checked')) {
                        _this.params.nochecked.remove($('input[type=checkbox][name=new]').eq(i).attr('programClassId'));
                         _this.params.nochecked=_this.params.nochecked.unique();
                     }else{
                        _this.params.nochecked.push($('input[type=checkbox][name=new]').eq(i).attr('programClassId'));
                        _this.params.nochecked=_this.params.nochecked.unique();
                     };
                };
                //去除x选取数组中未选取的ID
                for (var i = 0; i < _this.params.nochecked.length; i++) {
                    // console.log(nochecked[i]);
                    _this.params.idArrUnique.remove(_this.params.nochecked[i]);
                };
                if ($('input[type=checkbox][name=new]:checked').length==$('#List tr').length) {
                     $('input[type=checkbox][name=all]').parent().addClass('checked');
                }else{
                    $('input[type=checkbox][name=all]').parent().removeClass('checked');
                };
                //console.log(_this.params.idArrUnique);
            });
        },500)
        $('.delete').on('click', function(event) {
            event.preventDefault();
            var programClassId=$(this).attr('programClassId');
            layui.use('layer', function(){
                layer.confirm('您确定要删除吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(index){
                    _this.deleteEvent(programClassId);
                    layer.close(index);
                }, function(){

                });
            });
        });


    },
    deleteEvent:function(programClassId){
        var _this = this;
        var Params = {
            passport: _this.params.passport,
            programClassId:programClassId,
            type:_this.params.type
        };
        console.log(Params);
        Api.newclassdelete(Params, function(data) {
            _this.params.page=1;
            _this.getData();

        })
    },
    allEvent:function(val){
        var _this = this;
        var programClassId='';
        if (_this.params.idArrUnique.length>0) {

            programClassId=_this.params.idArrUnique.join(",");
        }
        console.log(programClassId);

        if (val==1) {
            if (_this.params.idArrUnique.length>0) {
                _this.deleteEvent(programClassId);

            }else{
                layui.use('layer', function(){
                    layer.msg('请先选择要删除的分类', {icon: 5});
                });
            };
        };
    },
    typeadd: function(className,isUse) {
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            className: className,
            isUse:isUse,
            type:_this.params.type,
        };
        Api.newclassadd(paramsObj, function(data) {
            console.log(data);
            _this.params.page=1;
            _this.getData();
            $('#modal_classify').modal('hide');
            _this.programclasslistget();
        });
    },
    programclasslistget:function(){
        var _this=this;
        var paramsObj = {
            passport: _this.params.passport,
            listRows:10000,
            page:1,
            type:_this.params.type
        };
        Api.newclasslist(paramsObj, function(data){
            console.log(data);
            _this.programclasslist(data.list)
        })
    },
    programclasslist:function(data){
        var _this=this;
        if (data && data.length > 0) {
            var html = '<option value="" selected>请选择分类</option>', i = 0, len = data.length;
            for (i; i < len; i++) {
                html+='<option value="'+data[i].className+'">'+data[i].className+'</option>';
            }
            $('#classifySelect').html(html);
            _this.programclasslistEvent();

        }
    },
    programclasslistEvent:function(){
        var _this=this;
        $('#classifySelect').on('change', function(event) {
            event.preventDefault();
            _this.params.className=$(this).val();
        });
    },
    otherEvent:function(){
        var _this = this;
        layui.use('laydate', function() {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#time',
                range: '~',
                done: function(value, date, endDate) {
                    console.log(date);
                    if (date.year) {
                        if (date.month < 10) {
                            date.month = "0" + date.month;
                        }
                        if (date.date < 10) {
                            date.date = "0" + date.date;
                        }
                        if (endDate.month < 10) {
                            endDate.month = "0" + endDate.month;
                        }
                        if (endDate.date < 10) {
                            endDate.date = "0" + endDate.date;
                        }
                        var gmtStart = date.year + '-' + date.month + '-' + date.date + ' 00:00:00';
                        var gmtEnd = endDate.year + '-' + endDate.month + '-' + endDate.date + ' 23:59:59';
                        _this.params.gmtStart = gmtStart;
                        _this.params.gmtEnd = gmtEnd;

                    } else {
                        _this.params.gmtStart = '';
                        _this.params.gmtEnd = '';
                    };
                }
            });
        });
        $('#normalSearch').click(function(event) {
            // _this.params.className=$('#className').val();
            _this.params.adminName=$('#adminName').val();
            _this.params.page=1;
            _this.getData();
        });
        $('.slectShow').click(function(event) {
            $('.select-wrap').toggle(400);
        });
        $('#changeSize').on('change', function(event) {
            event.preventDefault();
            _this.params.listRows = $(this).val();
            _this.params.page=1;
            $('#page').html('');
            _this.getData();
        });
        $('#changeDesc').on('change', function(event) {
            event.preventDefault();
            if ($(this).val() !== '排序方式') {
                _this.params.order = $(this).val();
                _this.params.page=1;
                $('#page').html('');
                _this.getData();
            };
        });
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('#selectYes').click(function(event) {
            _this.allEvent($('#allTypeSelect').val());
        });

        $('#typeYes').click(function(event) {
            if ($('#isUse').hasClass('switch-off')) {
                var isUse=0;
            }else{
                var isUse=1;
            };
            _this.typeadd($('#typeName').val(),isUse);
        });

        $('#add_classify').click(function(event) {
            $('#modal_classify').modal('show');
            $('#typeName').val('');
        });
    },
    initEvent: function(){
        var _this = this;
        _this.programclasslistget();
        _this.getData();
        _this.otherEvent();


    }
};
var page = new Page();